'use strict';

angular.module('app')
	.controller('loadPageController', [ '$rootScope', '$scope', '$http', '$state',
	                                function($rootScope,$scope, $http, $state) {
		$scope.title = 'H5落地页';
        $scope.searchParams = { };
        $scope.loading = false;
                                        $scope.loadPageInfo={};
		$scope.search = function () {
	        $scope.loading = true;
			$.ajax({
				url : $scope.app.project_name+ '/loadPage/list',
				data: $scope.searchParams
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		};

		$scope.searchRe = function () {
			$scope.loading = true;

			$scope.searchParams.loadPageName= $("#loadPageName").val();

            $.ajax({
                url : $scope.app.project_name+ '/loadPage/list',
                data: $scope.searchParams
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                    $scope.pageInfo = result.data;
                } else {
                    $scope.msg = result.msg;
                }
                $scope.$apply();
            });
		};

		$scope.queryMessage = function(pageLoadId){                        //查询信息收集接口
            // $scope.loading = true;
            $scope.searchParams = {};
            $scope.searchParams.pageLoadId = pageLoadId;
            $scope.searchParams.loadPageNum = $scope.loadPageNum;
            $.ajax({
                url : $scope.app.project_name+ '/loadPage/messageList',
                data: $scope.searchParams
            }).then(function(result) {
            	$scope.$apply(function () {
                    // $scope.loading = false;
                    if (result.httpCode == 200) {
                        json_array(result.data.records);
                        $scope.loadPageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                })

            });
		};

		function json_array(data){                    // 处理接口返回的数据方法
			var len=eval(data).length;
			$scope.allData = [];
			$scope.keyList = [];
			$scope.valueList = [];
			for(var i=0;i<len;i++){                     // 转json为数组
                var parse = JSON.parse(data[i].message);
                parse.push({'创建时间':data[i].createTime});
                $scope.allData.push(parse);
			}
			for(var i=0;i<$scope.allData.length;i++){              // 循环数组，获取到数组内对象的key 和value
				for(var j=0;j<$scope.allData[i].length;j++){
					for(var k in $scope.allData[i][j]){
						if(i==0){
							$scope.keyList.push(k);
						}
					}
				}
			}
			$scope.dataArray=$scope.allData;
            // $scope.$apply();
        }

		$scope.jion=function(v){
			if(Object.prototype.toString.call(v)=='[object Array]'){
				return v.join(',')
			}else{
				return v
			}
		};

		$scope.jion([1,1]);

		$scope.loadPagination = function (pageNo) {
			$scope.loadPageNum = pageNo;
			$scope.queryMessage($scope.searchParams.pageLoadId);
		};

		$scope.search();
		
		$scope.clearSearch = function() {
			$scope.searchParams= {};
			$scope.search();
		};
		
		$scope.disableItem = function(id, enable) {
			
		};

		// 翻页
		$scope.pagination = function (pageNo) {
			$scope.searchParams.pageNum=pageNo;
			$scope.search();
		};

		$scope.inforModule = function(v,i){        // 点击弹出信息收集器弹窗
            if(i=='0'){
			}else{
				$scope.isShow = true;
				$scope.queryMessage(v)
			}
			$scope.queryId = v
		}

		$scope.closeShow = function(){          // 点击关闭信息收集器弹窗
			$scope.isShow = false;
		}


} ]);